﻿/// <reference name="MicrosoftAjax.js"/>
// Function:    js公用函数
// FileName:    WebCommon
// Version:     1.0.0.0
// FileVersion: 1.0.2009.1
// Date:        2009/07/29
// Author:      胡进平
//!-----------------------------------------------------------------------
//! Copyright (C) Hjp Corporation. All rights reserved.
//!-----------------------------------------------------------------------
function WebCommon() { 
}

WebCommon.stopBubble=function(e) {
    if (e && e.stopPropagation)
        e.stopPropagation();
    else
        window.event.cancelBubble = true;
}

WebCommon.stopDefault=function(e) {
    if (e && e.preventDefault)
        e.preventDefault();
    else
        window.event.returnValue = false;
    return false;
}

WebCommon.getPosition = function(element) {
    if (element.self || element.nodeType === 9) return { x: 0, y: 0 };

    var clientRects = element.getClientRects();
    if (!clientRects || !clientRects.length) {
        return { x: 0, y: 0 };
    }

    var w = element.ownerDocument.parentWindow;
    var offsetL = w.screenLeft - top.screenLeft - top.document.documentElement.scrollLeft + 2;
    var offsetT = w.screenTop - top.screenTop - top.document.documentElement.scrollTop + 2;

    var f = w.frameElement || null;
    if (f) {
        var fstyle = f.currentStyle;
        offsetL += (f.frameBorder || 1) * 2 +
                        (parseInt(fstyle.paddingLeft) || 0) +
                        (parseInt(fstyle.borderLeftWidth) || 0) -
                        element.ownerDocument.documentElement.scrollLeft;
        offsetT += (f.frameBorder || 1) * 2 +
                        (parseInt(fstyle.paddingTop) || 0) +
                        (parseInt(fstyle.borderTopWidth) || 0) -
                        element.ownerDocument.documentElement.scrollTop;
    }

    var clientRect = clientRects[0];

    return { x: clientRect.left - offsetL,
        y: clientRect.top - offsetT
    };
}

WebCommon.getBounds = function(element) {
    var offset = WebCommon.getPosition(element);
    return { x: offset.x, y: offset.y, width: element.offsetWidth || 0, height: element.offsetHeight || 0 };
}

WebCommon.getCoords = function(el) {
    var box = el.getBoundingClientRect(),
    doc = el.ownerDocument,
    body = doc.body,
    html = doc.documentElement,
    clientTop = html.clientTop || body.clientTop || 0,
    clientLeft = html.clientLeft || body.clientLeft || 0,
    top = box.top + (self.pageYOffset || html.scrollTop || body.scrollTop) - clientTop,
    left = box.left + (self.pageXOffset || html.scrollLeft || body.scrollLeft) - clientLeft
    return { 'top': top, 'left': left };
};

WebCommon.xmlDocument = function() {
    var xmldoc = null;
    if (window.ActiveXObject) {
        var arrSignatures = ["MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0",
                             "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument",
                             "Microsoft.XmlDom"];

        for (var i = 0; i < arrSignatures.length; i++) {
            try {

                var xmldoc = new ActiveXObject(arrSignatures[i]);

                return xmldoc;

            } catch (oError) {
                //ignore
            }
        }

        throw new Error("MSXML is not installed on your system.");
    }
    else {
        xmldoc = document.implementation.createDocument("", "doc", null)
    }
    return xmldoc;
};
/// <summary>
/// 计算（中英文）字符长度
/// </summary>
WebCommon.strlen = function(str) {
    var len = 0;
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255) len += 2; else len++;
    }
    return len;
};

/// <summary>
/// 截取指定长度的（中英文）字符
/// </summary>
WebCommon.curStr=function(str, length) {
    var position = 0;
    var result = [];
    for (var i = 0; i < str.length; i++) {
        if (position >= length)
            break;
        if (str.charCodeAt(i) > 255) {
            position += 2;
            result.push(str.substr(i, 1));
        }
        else {
            position++;
            result.push(str.substr(i, 1));
        }
    }
    return result.join("");
}

WebCommon.trimStart = function(str, chr) {
    if (str.length == 0) return '';
    if (str.substr(0, 1) == chr) str = str.substr(1);
    return str;
};

WebCommon.trimEnd = function(str, chr) {
    if (str.length == 0) return '';
    if (str.substr(str.length - 1, 1) == chr) str = str.substr(0, str.length - 1);
    return str;
};

WebCommon.trim = function(str, chr) {
    if (str.length == 0) return '';
    if (str.substr(0, 1) == chr) str = str.substr(1);
    if (str.substr(str.length - 1, 1) == chr) str = str.substr(0, str.length - 1);
    return str;
};

WebCommon.parseDate = function(strDate) {
    var d = [];
    if (strDate.indexOf('-') > 0) {
        d.push(strDate.split('-')[2]);
        d.push(strDate.split('-')[1]);
        d.push(strDate.split('-')[0]);
        return Date.parse(d.join('/'));
    }
    return null;
};

WebCommon.Round = function(number, pos) {
    if (isNaN(number) || isNaN(pos)) {
        alert("不是数字");
        return;
    }

    var n = new Number(number);
    var s = Math.pow(10, pos) * n;
    var t = Math.round(s);
    return t / Math.pow(10, pos);
};

WebCommon.showModalWindow = function(url, name, width, height)
{
    if (window.showModalDialog) {
        var properties = "dialogWidth:" + width + "px;dialogHeight:" + height + "px;resizable:yes;scroll=auto;";
        return window.showModalDialog(url, name, properties);
    }
    else {
        var properties = 'height=' + height + ',width=' + width + ',toolbar=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=yes,modal=yes';
        return window.open(url, name, properties);
    }
};

var $Common = WebCommon;